// pages/functionalimpairment/functionalimpairment.js
import { ajax, src, util } from "../../utils/util.js";
// var option = '';//问题答案id

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uqid:'',
    question1:[],
    csimg:'../images/per_03.png',//选中的人像图
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var problem = [];
    wx.setStorageSync("problem", problem)
    var uqid = wx.getStorageSync("uqid")
    this.setData({
      uqid: uqid,
    });
    this.getQuestion();
  },

  //获取问卷1
  getQuestion :function(){
    var pid = wx.getStorageSync("pid")
    ajax("/api/index/question", {
      data: {
        uqid: this.data.uqid,
        pid: pid,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'GET'
    }, res => {

      if (res.error == 0) {
        var question1 = res.data[1];
        for (var i = 0; i < question1.data.length;i++){
          var data = question1.data[i];
          if (data.list.length != 0){
            for (var j = 0; j < data.list.length; j++) {
              if (data.is_genre == 1 || data.is_genre == 2) {
                data.list[j].isChecked = false;
              }
            }
          }
        }
        this.setData({
          question1: question1
        })
        console.log(question1)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  //位置 - 多选
  clickPos: function (e) {
    var question1 = this.data.question1;

    var index = e.currentTarget.dataset.index;
    var qid = e.currentTarget.dataset.qid;
    var optionid = e.currentTarget.dataset.optionid;
    var is_edit = e.currentTarget.dataset.is_edit;


    var problem = wx.getStorageSync("problem");

    for (var i = 0; i < question1.data.length; i++) {
      var data = question1.data[i];
      if (data.list.length != 0) {
        if (qid == data.id){
          for (var j = 0; j < data.list.length; j++) {
            if (index == j){
              

              if (data.list[j].isChecked == true) {
                this.setData({
                  csimg: '../images/per_03.png'
                })
              }
              data.list[j].isChecked = !data.list[j].isChecked;
              if (problem == '' || problem == null || problem == undefined || problem.length == 0) {
                var pro = {
                  id: qid,
                  option: optionid+",",
                  is_edit: is_edit,
                  fill_content: ''
                }
                problem.push(pro);

              } else {

                var is_qid = problem.some(({ id }) => id == qid)
                if (is_qid == true) {
                  for (var x = 0; x < problem.length; x++) {
                    if (problem[x].id == qid) {
                      if (optionid == data.list[j].id && data.list[j].isChecked == false) {

                        problem[x].option = problem[x].option.replace(optionid + "," , "");

                      } else if(optionid == data.list[j].id && data.list[j].isChecked == true){

                        problem[x].option += optionid + ",";

                      }
                      if (problem[x].option == ''){
                        problem.splice(x, 1);
                      }
                    }
                  }

                } else {
                  var pro = {
                    id: qid,
                    option: optionid+",",
                    is_edit: is_edit,
                    fill_content: ''
                  }
                  problem.push(pro);
                }
              }
              wx.setStorageSync("problem", problem)
              //人体图
              if (data.list[j].image != null) {
                for(var v = 0; v<problem.length; v++){
                  if (problem[v].id = data.list[j].id){
                    var option_id_img = problem[v].option.substring(0, problem[i].option.length - 1);
                    ajax("/api/index/cs", {
                      data: {
                        uqid: this.data.uqid,
                        option_id: option_id_img,
                      },
                      header: {
                        'Accept': 'application/json', // 默认值
                      },
                      method: 'GET'
                    }, res => {
                      console.log(res)
                      if (res.error == 0) {
                        this.setData({
                          csimg:res.data.image
                        })
                      }

                    })
                  }
                }
              }
            }
          }
        }
      }
    }
    this.setData({
      question1: question1
    })
    
  }, 
  //多选的 输入框
  otherChange:function(e){
    var problem = wx.getStorageSync("problem");
    var question1 = this.data.question1;
    var qid = e.currentTarget.dataset.qid;
  
    if (problem == '' || problem == null || problem == undefined || problem.length == 0) {
      var pro = {
        id: e.currentTarget.dataset.qid,
        option: '',
        is_edit: e.currentTarget.dataset.is_edit,
        fill_content: e.detail.value 
      }
      problem.push(pro);

    } else {
      var is_qid = problem.some(({ id }) => id == qid)
      if (is_qid == true) {
        for (var x = 0; x < problem.length; x++) {
          if (problem[x].id == qid) {
            problem[x].fill_content = e.detail.value;
          }
        }
      } else {
        var pro = {
          id: e.currentTarget.dataset.qid,
          option: '',
          is_edit: e.currentTarget.dataset.is_edit,
          fill_content: e.detail.value
        }
        problem.push(pro);
      }
    }

    wx.setStorageSync("problem", problem);
  },
  



// var now_options = wx.getStorageSync("problem_" + is_edit+'_'+qid);
// if (now_options == '' || now_options == null || now_options == undefined || now_options.length==0){
//   now_options = {
//     id: qid,
//     option: [optionid],
//     is_edit: is_edit,
//     fill_content: ''
//   };
// }else{
//   now_options.option.push(optionid);
// }
// wx.setStorageSync("problem_" + is_edit + '_' + qid, now_options)
// console.log(now_options)

//  [ [id: 1, is_e: 1], [id: 2, is_e: 0] ]

//   var now_options = wx.getStorageSync("problem_" + is_edit + '_' + qid);
  //  -下拉单选
  bindPickerChange: function (e) {
    this.setData({
      distype: e.detail.value
    })
    var question1 = this.data.question1;
    var qid = '';
    var optionid = '';
    var is_edit = '';
    var problem = wx.getStorageSync("problem");

    for (var i = 0; i < question1.data.length; i++) {
      var data = question1.data[i];
      if (data.is_genre == 1 && data.is_style == 1) {
        var qid = data.id;
        var optionid = data.list[this.data.distype].id;
        var is_edit = data.is_edit;

        if (problem == '' || problem == null || problem == undefined || problem.length == 0) {
          var pro = {
            id: qid,
            option: optionid,
            is_edit: is_edit,
            fill_content: ''
          }
          problem.push(pro);

        } else {
          var is_qid = problem.some(({ id }) => id == qid)
          if (is_qid == true) {
            for (var x = 0; x < problem.length; x++) {
              if (problem[x].id == qid) {
                problem[x].option = optionid;
              }
            }
          } else {
            var pro = {
              id: qid,
              option: optionid,
              is_edit: is_edit,
              fill_content: ''
            }
            problem.push(pro);
          }
        }

      }
    }
    wx.setStorageSync('problem', problem);
  },
  //病情状况  点击 -单选
  clickSym: function (e) {

    var question1 = this.data.question1;
    var qid = e.currentTarget.dataset.qid;
    var optionid = e.currentTarget.dataset.optionid;
    var is_edit = e.currentTarget.dataset.is_edit;
    var fill_content = '';
    var index = e.currentTarget.dataset.index;


    var problem = wx.getStorageSync("problem");

    for (var i = 0; i < question1.data.length; i++) {
      var data = question1.data[i];
      if (data.is_genre == 1 && data.is_style == 0) {
        if (data.list.length != 0) {
          if (qid == data.id) {
            for (var j = 0; j < data.list.length; j++) {
              if (data.list[j].id == optionid) {
                data.list[j].isChecked = true;
              } else {
                data.list[j].isChecked = false;
              }
            }
          }
        }
      }
    }
    this.setData({
      question1: question1
    })

    if (problem == '' || problem == null || problem == undefined || problem.length == 0) {
      var pro = {
        id: qid,
        option: optionid,
        is_edit: is_edit,
        fill_content: ''
      }
      problem.push(pro);

    } else {
      var is_qid = problem.some(({ id }) => id == qid)
      if (is_qid == true) {
        for (var x = 0; x < problem.length; x++) {
          if (problem[x].id == qid) {
            problem[x].option = optionid;
          }
        }
      } else {
        var pro = {
          id: qid,
          option: optionid,
          is_edit: is_edit,
          fill_content: ''
        }
        problem.push(pro);
      }
    }

    wx.setStorageSync('problem', problem);

  },
  //临床诊断  填空 --输入
  diagnosisChange: function (e) {
    var problem = wx.getStorageSync("problem");
    var question1 = this.data.question1;
    var qid = e.currentTarget.dataset.qid;
    var is_genre = e.currentTarget.dataset.is_genre;
    var is_style = e.currentTarget.dataset.is_style;

    if (is_genre == 3 && is_style == 1) { //时间
      this.setData({
        date: e.detail.value
      })
    }

    if (problem == '' || problem == null || problem == undefined || problem.length == 0) {
      var pro = {
        id: e.currentTarget.dataset.qid,
        option: e.detail.value,
        is_edit: e.currentTarget.dataset.is_edit,
        fill_content: ''
      }
      problem.push(pro);

    } else {
      var is_qid = problem.some(({ id }) => id == qid)
      if (is_qid == true) {
        for (var x = 0; x < problem.length; x++) {
          if (problem[x].id == qid) {
            problem[x].option = e.detail.value;
          }
        }
      } else {
        var pro = {
          id: e.currentTarget.dataset.qid,
          option: e.detail.value,
          is_edit: e.currentTarget.dataset.is_edit,
          fill_content: ''
        }
        problem.push(pro);
      }
    }

    wx.setStorageSync("problem", problem);
  },

  //下一步
  nextClick: function () {
    console.log(wx.getStorageSync("problem"));

    if (wx.getStorageSync("problem").length < this.data.question1.data.length){
      wx.showToast({
        title: '请您填写完整信息',
        icon:'none',
        duration:1000
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/clinicalhistory/clinicalhistory',
    })
  }
})